package com.luokangtao.order.controller;


import com.alibaba.fastjson.JSONObject;
import com.luokangtao.common.annotation.GetApi;
import com.luokangtao.common.annotation.PostApi;
import com.luokangtao.common.entity.Result;
import com.luokangtao.common.handler.BaseException;
import com.luokangtao.common.util.RedisLockUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.logging.SimpleFormatter;

/**
 * 订单服务
 * @author luokangtao
 * @create 2019-07-28 13:03
 */
@Api(value = "order订单服务",tags = "order订单服务")
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    RedisLockUtil redisLockUtil;

    @GetApi(value = "/{s}",auth = false)
    @ApiOperation("单机测试")
    public Result test(@ApiParam(name = "s",value = "测试字符串",example = "1",required = true)@PathVariable("s") String s){
        JSONObject jsonObject = JSONObject.parseObject(s);
        log.info("jsonObject:"+jsonObject);
        JSONObject info = (JSONObject) jsonObject.get("currentUserInfo");
        log.info("info",info);
        String realName = (String) info.get("realName");
        log.info("realName:"+realName);
        return Result.ok("单机测试成功");
    }

    @GetApi(auth = false)
    @ApiOperation("分布式锁测试")
    public Result redisResult(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info("当前时间:"+ simpleDateFormat.format(new Date()));
        //初始化key值
        String userId="19920511";
        //参数1:key 参数2:数值(10秒) 参数3:单位是秒
        boolean look = redisLockUtil.lock(userId, 10, TimeUnit.SECONDS);
        //如果上锁成功
        if(look){
            try {
                //业务处理
            }finally {
                //释放锁
                redisLockUtil.unLock("lkt");
            }
        }else {
            //如果当前已经存在锁的话
            throw new BaseException("请不要频繁操作");
        }
        return Result.ok("分布式锁测试成功");
    }
}
